package mdv.exp.items.chooser;

import java.util.zip.Inflater;

import com.example.data.MyDataBaseHelper;
import com.example.expenses.ItemsActivity;
import com.example.expenses.R;
import com.example.expenses.R.drawable;
import com.example.expenses.R.layout;
import com.example.expenses.R.string;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ItemsChooserFragment extends DialogFragment implements OnClickListener {

	private static final String TAG = "ItemChooserFragment";
	private ItemsChooserAdapter adapter;
	private OnMultiChoiceClickListener listener;
	private ItemsActivity activity;
	private Context context = getActivity();
	private int fragmentNum;
	private String title;

	 /**
     * Create a new instance of MyDialogFragment, providing "num"
     * as an argument.
     */
    public static ItemsChooserFragment newInstance(int num) {
    	ItemsChooserFragment f = new ItemsChooserFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

	
	
	@Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
		    fragmentNum = getArguments().getInt("num");
		    ItemsActivity activity = (ItemsActivity)getActivity();
		    ItemsChooserListView chooserListView = null;
		    ItemsChooserAdapter chooserAdapter;
		    LayoutInflater inflater = activity.getLayoutInflater();
		    LinearLayout layout = null;
		    switch (fragmentNum) {
			case 0:
				title = getResources().getString(R.string.supplier);
				// Set the icon
				 layout = (LinearLayout)inflater.inflate(R.layout.item_chooser_layout, null);
				chooserListView = (ItemsChooserListView)(layout).getChildAt(0);
				chooserAdapter = new ItemsChooserAdapter(activity, 0, activity.SUPPLIER_HEADER_TAG);
				chooserListView.setAdapter(chooserAdapter);
				break;
			case 1:
				title = getResources().getString(R.string.kind);
				// Set the icon
				 layout = (LinearLayout)inflater.inflate(R.layout.item_chooser_layout, null);
				chooserListView = (ItemsChooserListView)(layout).getChildAt(0);
				chooserAdapter = new ItemsChooserAdapter(activity, 0, activity.KIND_HEADER_TAG);
				chooserListView.setAdapter(chooserAdapter);
				break;	

			default:
				break;
			}
			Log.d(TAG,"onCreateDialog: " + fragmentNum);
	        // Use the Builder class for convenient dialog construction
	        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			
			
			builder
	               .setTitle(title)
	               .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                       Log.d(TAG,"PID " + id);
	                   }
	               })
	               .setNegativeButton(R.string.cancel_action, new DialogInterface.OnClickListener() {
	                   public void onClick(DialogInterface dialog, int id) {
	                	   Log.d(TAG,"NID " + id);
	                   }
	               })
	        // Create the AlertDialog object and return it
	       .setIcon(getResources().getDrawable(R.drawable.btn_dropdown_up))
	       .setView(layout)
	       //.setView(chooserListView);
	        ;

	        AlertDialog alert = builder.create();
	       // alert.getListView().setAdapter(adapter);
	        return alert;
	    }
	
//	@Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//            Bundle savedInstanceState) {
//		Log.d(TAG,"onCreateView");
//		
//		//ItemsChooserAdapter chooserAdapter = new ItemsChooserAdapter(activity, 0, activity.SUPPLIER_HEADER_TAG);
//		//chooserListView.setAdapter(chooserAdapter);
//	 	
//	 	 
//	 
//	 return chooserListView;
// }

	public void setAdapter(ItemsChooserAdapter adapter) {
		this.adapter = adapter;
		
	}
	
	public void setActivity(Activity activity){
		this.activity = (ItemsActivity)activity;
	}


	@Override
	public void onClick(DialogInterface dialog, int which) {
		Log.d(TAG,"Cklicked: " + which);
		
	}

}
